package com.qfedu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @version 1.0
 * 公众号：Java架构栈
 * @Author: 卓不凡
 */
@Controller
@RequestMapping("test")
public class TestController {

    @RequestMapping("voidShow")
    public void  voidShow(HttpServletRequest request, HttpServletResponse response) throws Exception{
        System.out.println("voidShow");
        //域对象  request  session page  applicationContext
        request.setAttribute("message","dg");
        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    }
    @RequestMapping("voidShow1")
    public void  voidShow1(HttpServletRequest request, HttpServletResponse response) throws Exception{
        System.out.println("voidShow1");
        //request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
        //使用重定向
        //response.sendRedirect("/WEB-INF/pages/success.jsp");
//        response.sendRedirect("/test/voidShow");
        response.sendRedirect("/view/success.jsp");
    }
    @RequestMapping("show2")
    public ModelAndView show2() throws Exception{
        System.out.println("show2");
        //ModelAndView  在处理适配器执行完之后会返回modelandview  模型和视图  由视图解析器进行解析
        //model 封装了数据   view 视图
        ModelAndView modelAndView = new ModelAndView();
        //设置model    ModelMap 是继承自LindkedHashMap   key=value
        modelAndView.addObject("msg","hzy");
        //设置视图
        modelAndView.setViewName("success");
        return modelAndView;
    }
    @RequestMapping("show3")
    public String show3(Map<String,Object> map) throws Exception{
        map.put("name","xx");
        return "success";
    }
    @RequestMapping("show4")
    public String show4(Model model) throws Exception{

        model.addAttribute("age",18);

        return "success";
    }
    @RequestMapping("show5")
    public String show5() throws Exception{
        System.out.println("Show5");
        //使用转发    forward:/指定资源路径  等同于request.getRequestDispatcher().forward
        return "forward:/WEB-INF/pages/success.jsp";
    }
    @RequestMapping("show6")
    public String show46() throws Exception{
        System.out.println("Show6");
        //使用转发    forward:/指定接口url
        return "forward:/test/show3";
    }
    @RequestMapping("show7")
    public String show7() throws Exception{
        System.out.println("Show6");
        //使用重定向    redirect:/指定接口url   等同于respose.sendRedirect()
        return "redirect:/test/show3";
    }
}
